/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.app.ad.service.impl;

import me.zhengjie.modules.app.ad.domain.AduserXHotel;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.app.ad.repository.AduserXHotelRepository;
import me.zhengjie.modules.app.ad.service.AduserXHotelService;
import me.zhengjie.modules.app.ad.service.dto.AduserXHotelDto;
import me.zhengjie.modules.app.ad.service.dto.AduserXHotelQueryCriteria;
import me.zhengjie.modules.app.ad.service.mapstruct.AduserXHotelMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author HaojieLi
* @date 2021-03-20
**/
@Service
@RequiredArgsConstructor
public class AduserXHotelServiceImpl implements AduserXHotelService {

    private final AduserXHotelRepository aduserXHotelRepository;
    private final AduserXHotelMapper aduserXHotelMapper;

    @Override
    public Map<String,Object> queryAll(AduserXHotelQueryCriteria criteria, Pageable pageable){
        Page<AduserXHotel> page = aduserXHotelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(aduserXHotelMapper::toDto));
    }

    @Override
    public List<AduserXHotelDto> queryAll(AduserXHotelQueryCriteria criteria){
        return aduserXHotelMapper.toDto(aduserXHotelRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public AduserXHotelDto findById(String id) {
        AduserXHotel aduserXHotel = aduserXHotelRepository.findById(id).orElseGet(AduserXHotel::new);
        ValidationUtil.isNull(aduserXHotel.getId(),"AduserXHotel","id",id);
        return aduserXHotelMapper.toDto(aduserXHotel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AduserXHotelDto create(AduserXHotel resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return aduserXHotelMapper.toDto(aduserXHotelRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AduserXHotel resources) {
        AduserXHotel aduserXHotel = aduserXHotelRepository.findById(resources.getId()).orElseGet(AduserXHotel::new);
        ValidationUtil.isNull( aduserXHotel.getId(),"AduserXHotel","id",resources.getId());
        aduserXHotel.copy(resources);
        aduserXHotelRepository.save(aduserXHotel);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            aduserXHotelRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<AduserXHotelDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AduserXHotelDto aduserXHotel : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("广告商用户ID", aduserXHotel.getUserId());
            map.put("酒店ID", aduserXHotel.getHotelId());
            map.put("酒店名称", aduserXHotel.getHotelName());
            map.put("酒店编号", aduserXHotel.getHotelNo());
            map.put("创建人ID", aduserXHotel.getCreateUserId());
            map.put("创建时间", aduserXHotel.getCreteTime());
            map.put("更新时间", aduserXHotel.getUpdateTime());
            map.put("数据状态", aduserXHotel.getIsEnable());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}